home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-24  |  58.7 KB  |  2,254 lines

  1. /* Reading and parsing of makefiles for GNU Make.
  2. Copyright (C) 1988,89,90,91,92,93,94,95,96,97 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "dep.h"
  21. #include "filedef.h"
  22. #include "job.h"
  23. #include "commands.h"
  24. #include "variable.h"
  25.  
  26. /* This is POSIX.2, but most systems using -DPOSIX probably don't have it.  */
  27. #ifdef    HAVE_GLOB_H
  28. #include <glob.h>
  29. #else
  30. #include "glob/glob.h"
  31. #endif
  32.  
  33. #ifndef WINDOWS32
  34. #ifndef _AMIGA
  35. #ifndef VMS
  36. #include <pwd.h>
  37. #else
  38. struct passwd *getpwnam PARAMS ((char *name));
  39. #endif
  40. #endif
  41. #endif /* !WINDOWS32 */
  42.  
  43. /* A `struct linebuffer' is a structure which holds a line of text.
  44.    `readline' reads a line from a stream into a linebuffer
  45.    and works regardless of the length of the line.  */
  46.  
  47. struct linebuffer
  48.   {
  49.     /* Note:  This is the number of bytes malloc'ed for `buffer'
  50.        It does not indicate `buffer's real length.
  51.        Instead, a null char indicates end-of-string.  */
  52.     unsigned int size;
  53.     char *buffer;
  54.   };
  55.  
  56. #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
  57. #define freebuffer(lb) free ((lb)->buffer)
  58.  
  59.  
  60. /* A `struct conditionals' contains the information describing
  61.    all the active conditionals in a makefile.
  62.  
  63.    The global variable `conditionals' contains the conditionals
  64.    information for the current makefile.  It is initialized from
  65.    the static structure `toplevel_conditionals' and is later changed
  66.    to new structures for included makefiles.  */
  67.  
  68. struct conditionals
  69.   {
  70.     unsigned int if_cmds;    /* Depth of conditional nesting.  */
  71.     unsigned int allocated;    /* Elts allocated in following arrays.  */
  72.     char *ignoring;        /* Are we ignoring or interepreting?  */
  73.     char *seen_else;        /* Have we already seen an `else'?  */
  74.   };
  75.  
  76. static struct conditionals toplevel_conditionals;
  77. static struct conditionals *conditionals = &toplevel_conditionals;
  78.  
  79.  
  80. /* Default directories to search for include files in  */
  81.  
  82. static char *default_include_directories[] =
  83.   {
  84. #if defined(WINDOWS32) && !defined(INCLUDEDIR)
  85. /*
  86.  * This completly up to the user when they install MSVC or other packages.
  87.  * This is defined as a placeholder.
  88.  */
  89. #define INCLUDEDIR "."
  90. #endif
  91.     INCLUDEDIR,
  92. #ifndef _AMIGA
  93.     "/usr/gnu/include",
  94.     "/usr/local/include",
  95.     "/usr/include",
  96. #endif
  97.     0
  98.   };
  99.  
  100. /* List of directories to search for include files in  */
  101.  
  102. static char **include_directories;
  103.  
  104. /* Maximum length of an element of the above.  */
  105.  
  106. static unsigned int max_incl_len;
  107.  
  108. /* The filename and pointer to line number of the
  109.    makefile currently being read in.  */
  110.  
  111. char *reading_filename;
  112. unsigned int *reading_lineno_ptr;
  113.  
  114. /* The chain of makefiles read by read_makefile.  */
  115.  
  116. static struct dep *read_makefiles = 0;
  117.  
  118. static int read_makefile PARAMS ((char *filename, int flags));
  119. static unsigned int readline PARAMS ((struct linebuffer *linebuffer, FILE *stream,
  120.             char *filename, unsigned int lineno));
  121. static unsigned int do_define PARAMS ((char *name, unsigned int namelen, enum variable_origin origin,
  122.             unsigned int lineno, FILE *infile, char *filename));
  123. static int conditional_line PARAMS ((char *line, char *filename, unsigned int lineno));
  124. static void record_files PARAMS ((struct nameseq *filenames, char *pattern, char *pattern_percent,
  125.             struct dep *deps, unsigned int commands_started, char *commands,
  126.             unsigned int commands_idx, int two_colon, char *filename,
  127.             unsigned int lineno, int set_default));
  128.  
  129. /* Read in all the makefiles and return the chain of their names.  */
  130.  
  131. struct dep *
  132. read_all_makefiles (makefiles)
  133.      char **makefiles;
  134. {
  135.   unsigned int num_makefiles = 0;
  136.  
  137.   if (debug_flag)
  138.     puts ("Reading makefiles...");
  139.  
  140.   /* If there's a non-null variable MAKEFILES, its value is a list of
  141.      files to read first thing.  But don't let it prevent reading the
  142.      default makefiles and don't let the default goal come from there.  */
  143.  
  144.   {
  145.     char *value;
  146.     char *name, *p;
  147.     unsigned int length;
  148.  
  149.     {
  150.       /* Turn off --warn-undefined-variables while we expand MAKEFILES.  */
  151.       int save = warn_undefined_variables_flag;
  152.       warn_undefined_variables_flag = 0;
  153.  
  154.       value = allocated_variable_expand ("$(MAKEFILES)");
  155.  
  156.       warn_undefined_variables_flag = save;
  157.     }
  158.  
  159.     /* Set NAME to the start of next token and LENGTH to its length.
  160.        MAKEFILES is updated for finding remaining tokens.  */
  161.     p = value;
  162.  
  163.     while ((name = find_next_token (&p, &length)) != 0)
  164.       {
  165.     if (*p != '\0')
  166.       *p++ = '\0';
  167.     (void) read_makefile (name,
  168.                   RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
  169.       }
  170.  
  171.     free (value);
  172.   }
  173.  
  174.   /* Read makefiles specified with -f switches.  */
  175.  
  176.   if (makefiles != 0)
  177.     while (*makefiles != 0)
  178.       {
  179.     struct dep *tail = read_makefiles;
  180.     register struct dep *d;
  181.  
  182.     if (! read_makefile (*makefiles, 0))
  183.       perror_with_name ("", *makefiles);
  184.  
  185.     /* Find the right element of read_makefiles.  */
  186.     d = read_makefiles;
  187.     while (d->next != tail)
  188.       d = d->next;
  189.  
  190.     /* Use the storage read_makefile allocates.  */
  191.     *makefiles = dep_name (d);
  192.     ++num_makefiles;
  193.     ++makefiles;
  194.       }
  195.  
  196.   /* If there were no -f switches, try the default names.  */
  197.  
  198.   if (num_makefiles == 0)
  199.     {
  200.       static char *default_makefiles[] =
  201. #ifdef VMS
  202.     /* all lower case since readdir() (the vms version) 'lowercasifies' */
  203.     { "makefile.vms", "gnumakefile", "makefile", 0 };
  204. #else
  205. #ifdef _AMIGA
  206.     { "GNUmakefile", "Makefile", "SMakefile", 0 };
  207. #else /* !Amiga && !VMS */
  208.     { "GNUmakefile", "makefile", "Makefile", 0 };
  209. #endif /* AMIGA */
  210. #endif /* VMS */
  211.       register char **p = default_makefiles;
  212.       while (*p != 0 && !file_exists_p (*p))
  213.     ++p;
  214.  
  215.       if (*p != 0)
  216.     {
  217.       if (! read_makefile (*p, 0))
  218.         perror_with_name ("", *p);
  219.     }
  220.       else
  221.     {
  222.       /* No default makefile was found.  Add the default makefiles to the
  223.          `read_makefiles' chain so they will be updated if possible.  */
  224.       struct dep *tail = read_makefiles;
  225.       /* Add them to the tail, after any MAKEFILES variable makefiles.  */
  226.       while (tail != 0 && tail->next != 0)
  227.         tail = tail->next;
  228.       for (p = default_makefiles; *p != 0; ++p)
  229.         {
  230.           struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
  231.           d->name = 0;
  232.           d->file = enter_file (*p);
  233.           d->file->dontcare = 1;
  234.           /* Tell update_goal_chain to bail out as soon as this file is
  235.          made, and main not to die if we can't make this file.  */
  236.           d->changed = RM_DONTCARE;
  237.           if (tail == 0)
  238.         read_makefiles = d;
  239.           else
  240.         tail->next = d;
  241.           tail = d;
  242.         }
  243.       if (tail != 0)
  244.         tail->next = 0;
  245.     }
  246.     }
  247.  
  248.   return read_makefiles;
  249. }
  250.  
  251. /* Read file FILENAME as a makefile and add its contents to the data base.
  252.  
  253.    FLAGS contains bits as above.
  254.  
  255.    FILENAME is added to the `read_makefiles' chain.
  256.  
  257.    Returns 1 if a file was found and read, 0 if not.  */
  258.  
  259. static int
  260. read_makefile (filename, flags)
  261.      char *filename;
  262.      int flags;
  263. {
  264.   static char *collapsed = 0;
  265.   static unsigned int collapsed_length = 0;
  266.   register FILE *infile;
  267.   struct linebuffer lb;
  268.   unsigned int commands_len = 200;
  269.   char *commands = (char *) xmalloc (200);
  270.   unsigned int commands_idx = 0;
  271.   unsigned int commands_started;
  272.   register char *p;
  273.   char *p2;
  274.   int len;
  275.   int ignoring = 0, in_ignored_define = 0;
  276.   int no_targets = 0;        /* Set when reading a rule without targets.  */
  277.   char *passed_filename = filename;
  278.  
  279.   struct nameseq *filenames = 0;
  280.   struct dep *deps;
  281.   unsigned int lineno = 1;
  282.   unsigned int nlines = 0;
  283.   int two_colon;
  284.   char *pattern = 0, *pattern_percent;
  285.  
  286.   int makefile_errno;
  287. #if defined (WINDOWS32) || defined (__MSDOS__)
  288.   int check_again;
  289. #endif
  290.  
  291. #define record_waiting_files()                              \
  292.   do                                          \
  293.     {                                           \
  294.       if (filenames != 0)                              \
  295.     record_files (filenames, pattern, pattern_percent, deps,          \
  296.               commands_started, commands, commands_idx,              \
  297.               two_colon, filename, lineno,                  \
  298.               !(flags & RM_NO_DEFAULT_GOAL));                       \
  299.       filenames = 0;                                  \
  300.       commands_idx = 0;                                  \
  301.       pattern = 0;                                  \
  302.     } while (0)
  303.  
  304. #ifdef    lint    /* Suppress `used before set' messages.  */
  305.   two_colon = 0;
  306. #endif
  307.   pattern_percent = 0;
  308.   commands_started = lineno;
  309.  
  310.   if (debug_flag)
  311.     {
  312.       printf ("Reading makefile `%s'", filename);
  313.       if (flags & RM_NO_DEFAULT_GOAL)
  314.     printf (" (no default goal)");
  315.       if (flags & RM_INCLUDED)
  316.     printf (" (search path)");
  317.       if (flags & RM_DONTCARE)
  318.     printf (" (don't care)");
  319.       if (flags & RM_NO_TILDE)
  320.     printf (" (no ~ expansion)");
  321.       puts ("...");
  322.     }
  323.  
  324.   /* First, get a stream to read.  */
  325.  
  326.   /* Expand ~ in FILENAME unless it came from `include',
  327.      in which case it was already done.  */
  328.   if (!(flags & RM_NO_TILDE) && filename[0] == '~')
  329.     {
  330.       char *expanded = tilde_expand (filename);
  331.       if (expanded != 0)
  332.     filename = expanded;
  333.     }
  334.  
  335.   infile = fopen (filename, "r");
  336.   /* Save the error code so we print the right message later.  */
  337.   makefile_errno = errno;
  338.  
  339.   /* If the makefile wasn't found and it's either a makefile from
  340.      the `MAKEFILES' variable or an included makefile,
  341.      search the included makefile search path for this makefile.  */
  342.   if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
  343.     {
  344.       register unsigned int i;
  345.       for (i = 0; include_directories[i] != 0; ++i)
  346.     {
  347. #ifdef _AMIGA
  348.       char *name = concat (include_directories[i], (include_directories[i][strlen(include_directories[i])-1] == ':') ? "" : "/", filename);
  349. #else
  350.       char *name = concat (include_directories[i], "/", filename);
  351. #endif
  352.       infile = fopen (name, "r");
  353.       if (infile == 0)
  354.         free (name);
  355.       else
  356.         {
  357.           filename = name;
  358.           break;
  359.         }
  360.     }
  361.     }
  362.  
  363.   /* Add FILENAME to the chain of read makefiles.  */
  364.   deps = (struct dep *) xmalloc (sizeof (struct dep));
  365.   deps->next = read_makefiles;
  366.   read_makefiles = deps;
  367.   deps->name = 0;
  368.   deps->file = lookup_file (filename);
  369.   if (deps->file == 0)
  370.     {
  371.       deps->file = enter_file (savestring (filename, strlen (filename)));
  372.       if (flags & RM_DONTCARE)
  373.     deps->file->dontcare = 1;
  374.     }
  375.   if (filename != passed_filename)
  376.     free (filename);
  377.   filename = deps->file->name;
  378.   deps->changed = flags;
  379.   deps = 0;
  380.  
  381.   /* If the makefile can't be found at all, give up entirely.  */
  382.  
  383.   if (infile == 0)
  384.     {
  385.       /* If we did some searching, errno has the error from the last
  386.      attempt, rather from FILENAME itself.  Restore it in case the
  387.      caller wants to use it in a message.  */
  388.       errno = makefile_errno;
  389.       return 0;
  390.     }
  391.  
  392.   reading_filename = filename;
  393.   reading_lineno_ptr = &lineno;
  394.  
  395.   /* Loop over lines in the file.
  396.      The strategy is to accumulate target names in FILENAMES, dependencies
  397.      in DEPS and commands in COMMANDS.  These are used to define a rule
  398.      when the start of the next rule (or eof) is encountered.  */
  399.  
  400.   initbuffer (&lb);
  401.  
  402.   while (!feof (infile))
  403.     {
  404.       lineno += nlines;
  405.       nlines = readline (&lb, infile, filename, lineno);
  406.  
  407.       /* Check for a shell command line first.
  408.      If it is not one, we can stop treating tab specially.  */
  409.       if (lb.buffer[0] == '\t')
  410.     {
  411.       /* This line is a probably shell command.  */
  412.       unsigned int len;
  413.  
  414.       if (no_targets)
  415.         /* Ignore the commands in a rule with no targets.  */
  416.         continue;
  417.  
  418.       /* If there is no preceding rule line, don't treat this line
  419.          as a command, even though it begins with a tab character.
  420.          SunOS 4 make appears to behave this way.  */
  421.  
  422.       if (filenames != 0)
  423.         {
  424.           if (ignoring)
  425.         /* Yep, this is a shell command, and we don't care.  */
  426.         continue;
  427.  
  428.           /* Append this command line to the line being accumulated.  */
  429.           p = lb.buffer;
  430.           if (commands_idx == 0)
  431.         commands_started = lineno;
  432.           len = strlen (p);
  433.           if (len + 1 + commands_idx > commands_len)
  434.         {
  435.           commands_len = (len + 1 + commands_idx) * 2;
  436.           commands = (char *) xrealloc (commands, commands_len);
  437.         }
  438.           bcopy (p, &commands[commands_idx], len);
  439.           commands_idx += len;
  440.           commands[commands_idx++] = '\n';
  441.  
  442.           continue;
  443.         }
  444.     }
  445.  
  446.       /* This line is not a shell command line.  Don't worry about tabs.  */
  447.  
  448.       if (collapsed_length < lb.size)
  449.     {
  450.       collapsed_length = lb.size;
  451.       if (collapsed != 0)
  452.         free (collapsed);
  453.       collapsed = (char *) xmalloc (collapsed_length);
  454.     }
  455.       strcpy (collapsed, lb.buffer);
  456.       /* Collapse continuation lines.  */
  457.       collapse_continuations (collapsed);
  458.       remove_comments (collapsed);
  459.  
  460.       /* strncmp is first to avoid dereferencing out into space.  */
  461. #define    word1eq(s, l)     (len == l && !strncmp (s, p, l))
  462.       p = collapsed;
  463.       while (isspace (*p))
  464.     ++p;
  465.       if (*p == '\0')
  466.     /* This line is completely empty.  */
  467.     continue;
  468.  
  469.       /* Find the end of the first token */
  470.       for (p2 = p+1; *p2 != '\0' && !isspace(*p2); ++p2)
  471.         {}
  472.       len = p2 - p;
  473.  
  474.       /* Find the start of the second token.  If it's a `:', jump past
  475.          preprocessor stuff since it can't be that--this allows targets named
  476.          `export', etc. */
  477.       while (isspace (*p2))
  478.         ++p2;
  479.       if (*p2 == '\0')
  480.         p2 = NULL;
  481.       else if (p2[0] == ':' && p2[1] == '\0')
  482.         goto check_var;
  483.  
  484.       /* We must first check for conditional and `define' directives before
  485.      ignoring anything, since they control what we will do with
  486.      following lines.  */
  487.  
  488.       if (!in_ignored_define
  489.       && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
  490.           || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
  491.           || word1eq ("else", 4) || word1eq ("endif", 5)))
  492.     {
  493.       int i = conditional_line (p, filename, lineno);
  494.       if (i >= 0)
  495.         ignoring = i;
  496.       else
  497.         makefile_fatal (filename, lineno,
  498.                 "invalid syntax in conditional");
  499.       continue;
  500.     }
  501.       else if (word1eq ("endef", 5))
  502.     {
  503.       if (in_ignored_define)
  504.         in_ignored_define = 0;
  505.       else
  506.         makefile_fatal (filename, lineno, "extraneous `endef'");
  507.       continue;
  508.     }
  509.       else if (word1eq ("define", 6))
  510.     {
  511.       if (ignoring)
  512.         in_ignored_define = 1;
  513.       else
  514.         {
  515.           p2 = next_token (p + 6);
  516.           /* Let the variable name be the whole rest of the line,
  517.          with trailing blanks stripped (comments have already been
  518.          removed), so it could be a complex variable/function
  519.          reference that might contain blanks.  */
  520.           p = index (p2, '\0');
  521.           while (isblank (p[-1]))
  522.         --p;
  523.           lineno = do_define (p2, p - p2, o_file,
  524.                   lineno, infile, filename);
  525.         }
  526.       continue;
  527.     }
  528.       else if (word1eq ("override", 8))
  529.     {
  530.       p2 = next_token (p + 8);
  531.       if (p2 == 0)
  532.         makefile_error (filename, lineno, "empty `override' directive");
  533.       if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
  534.         {
  535.           if (ignoring)
  536.         in_ignored_define = 1;
  537.           else
  538.         {
  539.           p2 = next_token (p2 + 6);
  540.           /* Let the variable name be the whole rest of the line,
  541.              with trailing blanks stripped (comments have already been
  542.              removed), so it could be a complex variable/function
  543.              reference that might contain blanks.  */
  544.           p = index (p2, '\0');
  545.           while (isblank (p[-1]))
  546.             --p;
  547.           lineno = do_define (p2, p - p2, o_override,
  548.                       lineno, infile, filename);
  549.         }
  550.         }
  551.       else if (!ignoring
  552.            && !try_variable_definition (filename, lineno,
  553.                         p2, o_override))
  554.         makefile_error (filename, lineno, "empty `override' directive");
  555.  
  556.       continue;
  557.     }
  558.  
  559.       if (ignoring)
  560.     /* Ignore the line.  We continue here so conditionals
  561.        can appear in the middle of a rule.  */
  562.     continue;
  563.       else if (word1eq ("export", 6))
  564.     {
  565.       struct variable *v;
  566.       p2 = next_token (p + 6);
  567.       if (*p2 == '\0')
  568.         export_all_variables = 1;
  569.       v = try_variable_definition (filename, lineno, p2, o_file);
  570.       if (v != 0)
  571.         v->export = v_export;
  572.       else
  573.         {
  574.           unsigned int len;
  575.           for (p = find_next_token (&p2, &len); p != 0;
  576.            p = find_next_token (&p2, &len))
  577.         {
  578.           v = lookup_variable (p, len);
  579.           if (v == 0)
  580.             v = define_variable (p, len, "", o_file, 0);
  581.           v->export = v_export;
  582.         }
  583.         }
  584.     }
  585.       else if (word1eq ("unexport", 8))
  586.     {
  587.       unsigned int len;
  588.       struct variable *v;
  589.       p2 = next_token (p + 8);
  590.       if (*p2 == '\0')
  591.         export_all_variables = 0;
  592.       for (p = find_next_token (&p2, &len); p != 0;
  593.            p = find_next_token (&p2, &len))
  594.         {
  595.           v = lookup_variable (p, len);
  596.           if (v == 0)
  597.         v = define_variable (p, len, "", o_file, 0);
  598.           v->export = v_noexport;
  599.         }
  600.     }
  601.       else if (word1eq ("vpath", 5))
  602.     {
  603.       char *pattern;
  604.       unsigned int len;
  605.       p2 = variable_expand (p + 5);
  606.       p = find_next_token (&p2, &len);
  607.       if (p != 0)
  608.         {
  609.           pattern = savestring (p, len);
  610.           p = find_next_token (&p2, &len);
  611.           /* No searchpath means remove all previous
  612.          selective VPATH's with the same pattern.  */
  613.         }
  614.       else
  615.         /* No pattern means remove all previous selective VPATH's.  */
  616.         pattern = 0;
  617.       construct_vpath_list (pattern, p);
  618.       if (pattern != 0)
  619.         free (pattern);
  620.     }
  621.       else
  622.     check_var:
  623.         if (word1eq ("include", 7) || word1eq ("-include", 8)
  624.            || word1eq ("sinclude", 8))
  625.     {
  626.       /* We have found an `include' line specifying a nested
  627.          makefile to be read at this point.  */
  628.       struct conditionals *save, new_conditionals;
  629.       struct nameseq *files;
  630.       /* "-include" (vs "include") says no error if the file does not
  631.          exist.  "sinclude" is an alias for this from SGI.  */
  632.       int noerror = p[0] != 'i';
  633.  
  634.       p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
  635.       if (*p == '\0')
  636.         {
  637.           makefile_error (filename, lineno,
  638.                   "no file name for `%sinclude'",
  639.                   noerror ? "-" : "");
  640.           continue;
  641.         }
  642.  
  643.       /* Parse the list of file names.  */
  644.       p2 = p;
  645.       files = multi_glob (parse_file_seq (&p2, '\0',
  646.                           sizeof (struct nameseq),
  647.                           1),
  648.                   sizeof (struct nameseq));
  649.       free (p);
  650.  
  651.       /* Save the state of conditionals and start
  652.          the included makefile with a clean slate.  */
  653.       save = conditionals;
  654.       bzero ((char *) &new_conditionals, sizeof new_conditionals);
  655.       conditionals = &new_conditionals;
  656.  
  657.       /* Record the rules that are waiting so they will determine
  658.          the default goal before those in the included makefile.  */
  659.       record_waiting_files ();
  660.  
  661.       /* Read each included makefile.  */
  662.       while (files != 0)
  663.         {
  664.           struct nameseq *next = files->next;
  665.           char *name = files->name;
  666.           free ((char *)files);
  667.           files = next;
  668.  
  669.           if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
  670.                       | (noerror ? RM_DONTCARE : 0)))
  671.           && ! noerror)
  672.         makefile_error (filename, lineno,
  673.                 "%s: %s", name, strerror (errno));
  674.         }
  675.  
  676.       /* Free any space allocated by conditional_line.  */
  677.       if (conditionals->ignoring)
  678.         free (conditionals->ignoring);
  679.       if (conditionals->seen_else)
  680.         free (conditionals->seen_else);
  681.  
  682.       /* Restore state.  */
  683.       conditionals = save;
  684.       reading_filename = filename;
  685.       reading_lineno_ptr = &lineno;
  686.     }
  687. #undef    word1eq
  688.       else if (try_variable_definition (filename, lineno, p, o_file))
  689.     /* This line has been dealt with.  */
  690.     ;
  691.       else if (lb.buffer[0] == '\t')
  692.     {
  693.       p = collapsed;    /* Ignore comments.  */
  694.       while (isblank (*p))
  695.         ++p;
  696.       if (*p == '\0')
  697.         /* The line is completely blank; that is harmless.  */
  698.         continue;
  699.       /* This line starts with a tab but was not caught above
  700.          because there was no preceding target, and the line
  701.          might have been usable as a variable definition.
  702.          But now it is definitely lossage.  */
  703.       makefile_fatal (filename, lineno,
  704.               "commands commence before first target");
  705.     }
  706.       else
  707.     {
  708.       /* This line describes some target files.  */
  709.  
  710.       char *cmdleft;
  711.  
  712.       /* Record the previous rule.  */
  713.  
  714.       record_waiting_files ();
  715.  
  716.       /* Search the line for an unquoted ; that is not after an
  717.              unquoted #.  */
  718.       cmdleft = find_char_unquote (lb.buffer, ";#", 0);
  719.       if (cmdleft != 0 && *cmdleft == '#')
  720.         {
  721.           /* We found a comment before a semicolon.  */
  722.           *cmdleft = '\0';
  723.           cmdleft = 0;
  724.         }
  725.       else if (cmdleft != 0)
  726.         /* Found one.  Cut the line short there before expanding it.  */
  727.         *cmdleft = '\0';
  728.  
  729.       collapse_continuations (lb.buffer);
  730.  
  731.       /* Expand variable and function references before doing anything
  732.          else so that special characters can be inside variables.  */
  733.       p = variable_expand (lb.buffer);
  734.  
  735.       if (cmdleft == 0)
  736.         /* Look for a semicolon in the expanded line.  */
  737.         cmdleft = find_char_unquote (p, ";", 0);
  738.  
  739.       if (cmdleft != 0)
  740.         /* Cut the line short at the semicolon.  */
  741.         *cmdleft = '\0';
  742.  
  743.       p2 = next_token (p);
  744.       if (*p2 == '\0')
  745.         {
  746.           if (cmdleft != 0)
  747.         makefile_fatal (filename, lineno,
  748.                 "missing rule before commands");
  749.           else
  750.         /* This line contained a variable reference that
  751.            expanded to nothing but whitespace.  */
  752.         continue;
  753.         }
  754.       else if (*p2 == ':')
  755.         {
  756.           /* We accept and ignore rules without targets for
  757.          compatibility with SunOS 4 make.  */
  758.           no_targets = 1;
  759.           continue;
  760.         }
  761.  
  762.       filenames = multi_glob (parse_file_seq (&p2, ':',
  763.                           sizeof (struct nameseq),
  764.                           1),
  765.                   sizeof (struct nameseq));
  766.       if (*p2++ == '\0')
  767.         makefile_fatal (filename, lineno, "missing separator");
  768.       /* Is this a one-colon or two-colon entry?  */
  769.       two_colon = *p2 == ':';
  770.       if (two_colon)
  771.         p2++;
  772.  
  773.       /* We have some targets, so don't ignore the following commands.  */
  774.       no_targets = 0;
  775.  
  776.       /* Is this a static pattern rule: `target: %targ: %dep; ...'?  */
  777.       p = index (p2, ':');
  778.       while (p != 0 && p[-1] == '\\')
  779.         {
  780.           register char *q = &p[-1];
  781.           register int backslash = 0;
  782.           while (*q-- == '\\')
  783.         backslash = !backslash;
  784.           if (backslash)
  785.         p = index (p + 1, ':');
  786.           else
  787.         break;
  788.         }
  789. #ifdef _AMIGA
  790.       /* Here, the situation is quite complicated. Let's have a look
  791.         at a couple of targets:
  792.  
  793.         install: dev:make
  794.  
  795.         dev:make: make
  796.  
  797.         dev:make:: xyz
  798.  
  799.         The rule is that it's only a target, if there are TWO :'s
  800.         OR a space around the :.
  801.       */
  802.       if (p && !(isspace(p[1]) || !p[1] || isspace(p[-1])))
  803.         p = 0;
  804. #endif
  805. #if defined (WINDOWS32) || defined (__MSDOS__)
  806.           do {
  807.             check_again = 0;
  808.             /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
  809.             if (p != 0 && (p[1] == '\\' || p[1] == '/') && isalpha (p[-1])) {
  810.               p = index(p + 1, ':');
  811.               check_again = 1;
  812.             }
  813.           } while (check_again);
  814. #endif
  815.       if (p != 0)
  816.         {
  817.           struct nameseq *target;
  818.           target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
  819.           ++p2;
  820.           if (target == 0)
  821.         makefile_fatal (filename, lineno, "missing target pattern");
  822.           else if (target->next != 0)
  823.         makefile_fatal (filename, lineno, "multiple target patterns");
  824.           pattern = target->name;
  825.           pattern_percent = find_percent (pattern);
  826.           if (pattern_percent == 0)
  827.         makefile_fatal (filename, lineno,
  828.                 "target pattern contains no `%%'");
  829.         }
  830.       else
  831.         pattern = 0;
  832.  
  833.       /* Parse the dependencies.  */
  834.       deps = (struct dep *)
  835.         multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
  836.             sizeof (struct dep));
  837.  
  838.       commands_idx = 0;
  839.       if (cmdleft != 0)
  840.         {
  841.           /* Semicolon means rest of line is a command.  */
  842.           unsigned int len = strlen (cmdleft + 1);
  843.  
  844.           commands_started = lineno;
  845.  
  846.           /* Add this command line to the buffer.  */
  847.           if (len + 2 > commands_len)
  848.         {
  849.           commands_len = (len + 2) * 2;
  850.           commands = (char *) xrealloc (commands, commands_len);
  851.         }
  852.           bcopy (cmdleft + 1, commands, len);
  853.           commands_idx += len;
  854.           commands[commands_idx++] = '\n';
  855.         }
  856.  
  857.       continue;
  858.     }
  859.  
  860.       /* We get here except in the case that we just read a rule line.
  861.      Record now the last rule we read, so following spurious
  862.      commands are properly diagnosed.  */
  863.       record_waiting_files ();
  864.       no_targets = 0;
  865.     }
  866.  
  867.   if (conditionals->if_cmds)
  868.     makefile_fatal (filename, lineno, "missing `endif'");
  869.  
  870.   /* At eof, record the last rule.  */
  871.   record_waiting_files ();
  872.  
  873.   freebuffer (&lb);
  874.   free ((char *) commands);
  875.   fclose (infile);
  876.  
  877.   reading_filename = 0;
  878.   reading_lineno_ptr = 0;
  879.  
  880.   return 1;
  881. }
  882.  
  883. /* Execute a `define' directive.
  884.    The first line has already been read, and NAME is the name of
  885.    the variable to be defined.  The following lines remain to be read.
  886.    LINENO, INFILE and FILENAME refer to the makefile being read.
  887.    The value returned is LINENO, updated for lines read here.  */
  888.  
  889. static unsigned int
  890. do_define (name, namelen, origin, lineno, infile, filename)
  891.      char *name;
  892.      unsigned int namelen;
  893.      enum variable_origin origin;
  894.      unsigned int lineno;
  895.      FILE *infile;
  896.      char *filename;
  897. {
  898.   struct linebuffer lb;
  899.   unsigned int nlines = 0;
  900.   unsigned int length = 100;
  901.   char *definition = (char *) xmalloc (100);
  902.   register unsigned int idx = 0;
  903.   register char *p;
  904.  
  905.   /* Expand the variable name.  */
  906.   char *var = (char *) alloca (namelen + 1);
  907.   bcopy (name, var, namelen);
  908.   var[namelen] = '\0';
  909.   var = variable_expand (var);
  910.  
  911.   initbuffer (&lb);
  912.   while (!feof (infile))
  913.     {
  914.       unsigned int len;
  915.  
  916.       lineno += nlines;
  917.       nlines = readline (&lb, infile, filename, lineno);
  918.  
  919.       collapse_continuations (lb.buffer);
  920.  
  921.       p = next_token (lb.buffer);
  922.       len = strlen (p);
  923.       if ((len == 5 || (len > 5 && isblank (p[5])))
  924.           && !strncmp (p, "endef", 5))
  925.     {
  926.       p += 5;
  927.       remove_comments (p);
  928.       if (*next_token (p) != '\0')
  929.         makefile_error (filename, lineno,
  930.                 "Extraneous text after `endef' directive");
  931.       /* Define the variable.  */
  932.       if (idx == 0)
  933.         definition[0] = '\0';
  934.       else
  935.         definition[idx - 1] = '\0';
  936.       (void) define_variable (var, strlen (var), definition, origin, 1);
  937.       free (definition);
  938.       freebuffer (&lb);
  939.       return (lineno + nlines);
  940.     }
  941.       else
  942.     {
  943.           len = strlen (lb.buffer);
  944.       /* Increase the buffer size if necessary.  */
  945.       if (idx + len + 1 > length)
  946.         {
  947.           length = (idx + len) * 2;
  948.           definition = (char *) xrealloc (definition, length + 1);
  949.         }
  950.  
  951.       bcopy (lb.buffer, &definition[idx], len);
  952.       idx += len;
  953.       /* Separate lines with a newline.  */
  954.       definition[idx++] = '\n';
  955.     }
  956.     }
  957.  
  958.   /* No `endef'!!  */
  959.   makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
  960.  
  961.   /* NOTREACHED */
  962.   return 0;
  963. }
  964.  
  965. /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
  966.    "ifneq", "else" and "endif".
  967.    LINE is the input line, with the command as its first word.
  968.  
  969.    FILENAME and LINENO are the filename and line number in the
  970.    current makefile.  They are used for error messages.
  971.  
  972.    Value is -1 if the line is invalid,
  973.    0 if following text should be interpreted,
  974.    1 if following text should be ignored.  */
  975.  
  976. static int
  977. conditional_line (line, filename, lineno)
  978.      char *line;
  979.      char *filename;
  980.      unsigned int lineno;
  981. {
  982.   int notdef;
  983.   char *cmdname;
  984.   register unsigned int i;
  985.  
  986.   if (*line == 'i')
  987.     {
  988.       /* It's an "if..." command.  */
  989.       notdef = line[2] == 'n';
  990.       if (notdef)
  991.     {
  992.       cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
  993.       line += cmdname[3] == 'd' ? 7 : 6;
  994.     }
  995.       else
  996.     {
  997.       cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
  998.       line += cmdname[2] == 'd' ? 6 : 5;
  999.     }
  1000.     }
  1001.   else
  1002.     {
  1003.       /* It's an "else" or "endif" command.  */
  1004.       notdef = line[1] == 'n';
  1005.       cmdname = notdef ? "endif" : "else";
  1006.       line += notdef ? 5 : 4;
  1007.     }
  1008.  
  1009.   line = next_token (line);
  1010.  
  1011.   if (*cmdname == 'e')
  1012.     {
  1013.       if (*line != '\0')
  1014.     makefile_error (filename, lineno,
  1015.             "Extraneous text after `%s' directive",
  1016.             cmdname);
  1017.       /* "Else" or "endif".  */
  1018.       if (conditionals->if_cmds == 0)
  1019.     makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
  1020.       /* NOTDEF indicates an `endif' command.  */
  1021.       if (notdef)
  1022.     --conditionals->if_cmds;
  1023.       else if (conditionals->seen_else[conditionals->if_cmds - 1])
  1024.     makefile_fatal (filename, lineno, "only one `else' per conditional");
  1025.       else
  1026.     {
  1027.       /* Toggle the state of ignorance.  */
  1028.       conditionals->ignoring[conditionals->if_cmds - 1]
  1029.         = !conditionals->ignoring[conditionals->if_cmds - 1];
  1030.       /* Record that we have seen an `else' in this conditional.
  1031.          A second `else' will be erroneous.  */
  1032.       conditionals->seen_else[conditionals->if_cmds - 1] = 1;
  1033.     }
  1034.       for (i = 0; i < conditionals->if_cmds; ++i)
  1035.     if (conditionals->ignoring[i])
  1036.       return 1;
  1037.       return 0;
  1038.     }
  1039.  
  1040.   if (conditionals->allocated == 0)
  1041.     {
  1042.       conditionals->allocated = 5;
  1043.       conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
  1044.       conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
  1045.     }
  1046.  
  1047.   ++conditionals->if_cmds;
  1048.   if (conditionals->if_cmds > conditionals->allocated)
  1049.     {
  1050.       conditionals->allocated += 5;
  1051.       conditionals->ignoring = (char *)
  1052.     xrealloc (conditionals->ignoring, conditionals->allocated);
  1053.       conditionals->seen_else = (char *)
  1054.     xrealloc (conditionals->seen_else, conditionals->allocated);
  1055.     }
  1056.  
  1057.   /* Record that we have seen an `if...' but no `else' so far.  */
  1058.   conditionals->seen_else[conditionals->if_cmds - 1] = 0;
  1059.  
  1060.   /* Search through the stack to see if we're already ignoring.  */
  1061.   for (i = 0; i < conditionals->if_cmds - 1; ++i)
  1062.     if (conditionals->ignoring[i])
  1063.       {
  1064.     /* We are already ignoring, so just push a level
  1065.        to match the next "else" or "endif", and keep ignoring.
  1066.        We don't want to expand variables in the condition.  */
  1067.     conditionals->ignoring[conditionals->if_cmds - 1] = 1;
  1068.     return 1;
  1069.       }
  1070.  
  1071.   if (cmdname[notdef ? 3 : 2] == 'd')
  1072.     {
  1073.       /* "Ifdef" or "ifndef".  */
  1074.       struct variable *v;
  1075.       register char *p = end_of_token (line);
  1076.       i = p - line;
  1077.       p = next_token (p);
  1078.       if (*p != '\0')
  1079.     return -1;
  1080.       v = lookup_variable (line, i);
  1081.       conditionals->ignoring[conditionals->if_cmds - 1]
  1082.     = (v != 0 && *v->value != '\0') == notdef;
  1083.     }
  1084.   else
  1085.     {
  1086.       /* "Ifeq" or "ifneq".  */
  1087.       char *s1, *s2;
  1088.       unsigned int len;
  1089.       char termin = *line == '(' ? ',' : *line;
  1090.  
  1091.       if (termin != ',' && termin != '"' && termin != '\'')
  1092.     return -1;
  1093.  
  1094.       s1 = ++line;
  1095.       /* Find the end of the first string.  */
  1096.       if (termin == ',')
  1097.     {
  1098.       register int count = 0;
  1099.       for (; *line != '\0'; ++line)
  1100.         if (*line == '(')
  1101.           ++count;
  1102.         else if (*line == ')')
  1103.           --count;
  1104.         else if (*line == ',' && count <= 0)
  1105.           break;
  1106.     }
  1107.       else
  1108.     while (*line != '\0' && *line != termin)
  1109.       ++line;
  1110.  
  1111.       if (*line == '\0')
  1112.     return -1;
  1113.  
  1114.       if (termin == ',')
  1115.     {
  1116.       /* Strip blanks after the first string.  */
  1117.       char *p = line++;
  1118.       while (isblank (p[-1]))
  1119.         --p;
  1120.       *p = '\0';
  1121.     }
  1122.       else
  1123.     *line++ = '\0';
  1124.  
  1125.       s2 = variable_expand (s1);
  1126.       /* We must allocate a new copy of the expanded string because
  1127.      variable_expand re-uses the same buffer.  */
  1128.       len = strlen (s2);
  1129.       s1 = (char *) alloca (len + 1);
  1130.       bcopy (s2, s1, len + 1);
  1131.  
  1132.       if (termin != ',')
  1133.     /* Find the start of the second string.  */
  1134.     line = next_token (line);
  1135.  
  1136.       termin = termin == ',' ? ')' : *line;
  1137.       if (termin != ')' && termin != '"' && termin != '\'')
  1138.     return -1;
  1139.  
  1140.       /* Find the end of the second string.  */
  1141.       if (termin == ')')
  1142.     {
  1143.       register int count = 0;
  1144.       s2 = next_token (line);
  1145.       for (line = s2; *line != '\0'; ++line)
  1146.         {
  1147.           if (*line == '(')
  1148.         ++count;
  1149.           else if (*line == ')')
  1150.         if (count <= 0)
  1151.           break;
  1152.         else
  1153.           --count;
  1154.         }
  1155.     }
  1156.       else
  1157.     {
  1158.       ++line;
  1159.       s2 = line;
  1160.       while (*line != '\0' && *line != termin)
  1161.         ++line;
  1162.     }
  1163.  
  1164.       if (*line == '\0')
  1165.     return -1;
  1166.  
  1167.       *line = '\0';
  1168.       line = next_token (++line);
  1169.       if (*line != '\0')
  1170.     makefile_error (filename, lineno,
  1171.             "Extraneous text after `%s' directive",
  1172.             cmdname);
  1173.  
  1174.       s2 = variable_expand (s2);
  1175.       conditionals->ignoring[conditionals->if_cmds - 1]
  1176.     = streq (s1, s2) == notdef;
  1177.     }
  1178.  
  1179.   /* Search through the stack to see if we're ignoring.  */
  1180.   for (i = 0; i < conditionals->if_cmds; ++i)
  1181.     if (conditionals->ignoring[i])
  1182.       return 1;
  1183.   return 0;
  1184. }
  1185.  
  1186. /* Remove duplicate dependencies in CHAIN.  */
  1187.  
  1188. void
  1189. uniquize_deps (chain)
  1190.      struct dep *chain;
  1191. {
  1192.   register struct dep *d;
  1193.  
  1194.   /* Make sure that no dependencies are repeated.  This does not
  1195.      really matter for the purpose of updating targets, but it
  1196.      might make some names be listed twice for $^ and $?.  */
  1197.  
  1198.   for (d = chain; d != 0; d = d->next)
  1199.     {
  1200.       struct dep *last, *next;
  1201.  
  1202.       last = d;
  1203.       next = d->next;
  1204.       while (next != 0)
  1205.     if (streq (dep_name (d), dep_name (next)))
  1206.       {
  1207.         struct dep *n = next->next;
  1208.         last->next = n;
  1209.         if (next->name != 0 && next->name != d->name)
  1210.           free (next->name);
  1211.         if (next != d)
  1212.           free ((char *) next);
  1213.         next = n;
  1214.       }
  1215.     else
  1216.       {
  1217.         last = next;
  1218.         next = next->next;
  1219.       }
  1220.     }
  1221. }
  1222.  
  1223. /* Record a description line for files FILENAMES,
  1224.    with dependencies DEPS, commands to execute described
  1225.    by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
  1226.    TWO_COLON is nonzero if a double colon was used.
  1227.    If not nil, PATTERN is the `%' pattern to make this
  1228.    a static pattern rule, and PATTERN_PERCENT is a pointer
  1229.    to the `%' within it.
  1230.  
  1231.    The links of FILENAMES are freed, and so are any names in it
  1232.    that are not incorporated into other data structures.  */
  1233.  
  1234. static void
  1235. record_files (filenames, pattern, pattern_percent, deps, commands_started,
  1236.           commands, commands_idx, two_colon, filename, lineno, set_default)
  1237.      struct nameseq *filenames;
  1238.      char *pattern, *pattern_percent;
  1239.      struct dep *deps;
  1240.      unsigned int commands_started;
  1241.      char *commands;
  1242.      unsigned int commands_idx;
  1243.      int two_colon;
  1244.      char *filename;
  1245.      unsigned int lineno;
  1246.      int set_default;
  1247. {
  1248.   struct nameseq *nextf;
  1249.   int implicit = 0;
  1250.   unsigned int max_targets, target_idx;
  1251.   char **targets = 0, **target_percents = 0;
  1252.   struct commands *cmds;
  1253.  
  1254.   if (commands_idx > 0)
  1255.     {
  1256.       cmds = (struct commands *) xmalloc (sizeof (struct commands));
  1257.       cmds->filename = filename;
  1258.       cmds->lineno = commands_started;
  1259.       cmds->commands = savestring (commands, commands_idx);
  1260.       cmds->command_lines = 0;
  1261.     }
  1262.   else
  1263.     cmds = 0;
  1264.  
  1265.   for (; filenames != 0; filenames = nextf)
  1266.     {
  1267.       register char *name = filenames->name;
  1268.       register struct file *f;
  1269.       register struct dep *d;
  1270.       struct dep *this;
  1271.       char *implicit_percent;
  1272.  
  1273.       nextf = filenames->next;
  1274.       free ((char *) filenames);
  1275.  
  1276.       implicit_percent = find_percent (name);
  1277.       implicit |= implicit_percent != 0;
  1278.  
  1279.       if (implicit && pattern != 0)
  1280.     makefile_fatal (filename, lineno,
  1281.             "mixed implicit and static pattern rules");
  1282.  
  1283.       if (implicit && implicit_percent == 0)
  1284.     makefile_fatal (filename, lineno, "mixed implicit and normal rules");
  1285.  
  1286.       if (implicit)
  1287.     {
  1288.       if (targets == 0)
  1289.         {
  1290.           max_targets = 5;
  1291.           targets = (char **) xmalloc (5 * sizeof (char *));
  1292.           target_percents = (char **) xmalloc (5 * sizeof (char *));
  1293.           target_idx = 0;
  1294.         }
  1295.       else if (target_idx == max_targets - 1)
  1296.         {
  1297.           max_targets += 5;
  1298.           targets = (char **) xrealloc ((char *) targets,
  1299.                         max_targets * sizeof (char *));
  1300.           target_percents
  1301.         = (char **) xrealloc ((char *) target_percents,
  1302.                       max_targets * sizeof (char *));
  1303.         }
  1304.       targets[target_idx] = name;
  1305.       target_percents[target_idx] = implicit_percent;
  1306.       ++target_idx;
  1307.       continue;
  1308.     }
  1309.  
  1310.       /* If there are multiple filenames, copy the chain DEPS
  1311.      for all but the last one.  It is not safe for the same deps
  1312.      to go in more than one place in the data base.  */
  1313.       this = nextf != 0 ? copy_dep_chain (deps) : deps;
  1314.  
  1315.       if (pattern != 0)
  1316.     /* If this is an extended static rule:
  1317.        `targets: target%pattern: dep%pattern; cmds',
  1318.        translate each dependency pattern into a plain filename
  1319.        using the target pattern and this target's name.  */
  1320.     if (!pattern_matches (pattern, pattern_percent, name))
  1321.       {
  1322.         /* Give a warning if the rule is meaningless.  */
  1323.         makefile_error (filename, lineno,
  1324.                 "target `%s' doesn't match the target pattern",
  1325.                 name);
  1326.         this = 0;
  1327.       }
  1328.     else
  1329.       {
  1330.         /* We use patsubst_expand to do the work of translating
  1331.            the target pattern, the target's name and the dependencies'
  1332.            patterns into plain dependency names.  */
  1333.         char *buffer = variable_expand ("");
  1334.  
  1335.         for (d = this; d != 0; d = d->next)
  1336.           {
  1337.         char *o;
  1338.         char *percent = find_percent (d->name);
  1339.         if (percent == 0)
  1340.           continue;
  1341.         o = patsubst_expand (buffer, name, pattern, d->name,
  1342.                      pattern_percent, percent);
  1343.         free (d->name);
  1344.         d->name = savestring (buffer, o - buffer);
  1345.           }
  1346.       }
  1347.  
  1348.       if (!two_colon)
  1349.     {
  1350.       /* Single-colon.  Combine these dependencies
  1351.          with others in file's existing record, if any.  */
  1352.       f = enter_file (name);
  1353.  
  1354.       if (f->double_colon)
  1355.         makefile_fatal (filename, lineno,
  1356.                 "target file `%s' has both : and :: entries",
  1357.                 f->name);
  1358.  
  1359.       /* If CMDS == F->CMDS, this target was listed in this rule
  1360.          more than once.  Just give a warning since this is harmless.  */
  1361.       if (cmds != 0 && cmds == f->cmds)
  1362.         makefile_error
  1363.           (filename, lineno,
  1364.            "target `%s' given more than once in the same rule.",
  1365.            f->name);
  1366.  
  1367.       /* Check for two single-colon entries both with commands.
  1368.          Check is_target so that we don't lose on files such as .c.o
  1369.          whose commands were preinitialized.  */
  1370.       else if (cmds != 0 && f->cmds != 0 && f->is_target)
  1371.         {
  1372.           makefile_error (cmds->filename, cmds->lineno,
  1373.                   "warning: overriding commands for target `%s'",
  1374.                   f->name);
  1375.           makefile_error (f->cmds->filename, f->cmds->lineno,
  1376.                   "warning: ignoring old commands for target `%s'",
  1377.                   f->name);
  1378.         }
  1379.  
  1380.       f->is_target = 1;
  1381.  
  1382.       /* Defining .DEFAULT with no deps or cmds clears it.  */
  1383.       if (f == default_file && this == 0 && cmds == 0)
  1384.         f->cmds = 0;
  1385.       if (cmds != 0)
  1386.         f->cmds = cmds;
  1387.       /* Defining .SUFFIXES with no dependencies
  1388.          clears out the list of suffixes.  */
  1389.       if (f == suffix_file && this == 0)
  1390.         {
  1391.           d = f->deps;
  1392.           while (d != 0)
  1393.         {
  1394.           struct dep *nextd = d->next;
  1395.            free (d->name);
  1396.            free ((char *)d);
  1397.           d = nextd;
  1398.         }
  1399.           f->deps = 0;
  1400.         }
  1401.       else if (f->deps != 0)
  1402.         {
  1403.           /* Add the file's old deps and the new ones in THIS together.  */
  1404.  
  1405.           struct dep *firstdeps, *moredeps;
  1406.           if (cmds != 0)
  1407.         {
  1408.           /* This is the rule with commands, so put its deps first.
  1409.              The rationale behind this is that $< expands to the
  1410.              first dep in the chain, and commands use $< expecting
  1411.              to get the dep that rule specifies.  */
  1412.           firstdeps = this;
  1413.           moredeps = f->deps;
  1414.         }
  1415.           else
  1416.         {
  1417.           /* Append the new deps to the old ones.  */
  1418.           firstdeps = f->deps;
  1419.           moredeps = this;
  1420.         }
  1421.  
  1422.           if (firstdeps == 0)
  1423.         firstdeps = moredeps;
  1424.           else
  1425.         {
  1426.           d = firstdeps;
  1427.           while (d->next != 0)
  1428.             d = d->next;
  1429.           d->next = moredeps;
  1430.         }
  1431.  
  1432.           f->deps = firstdeps;
  1433.         }
  1434.       else
  1435.         f->deps = this;
  1436.  
  1437.       /* If this is a static pattern rule, set the file's stem to
  1438.          the part of its name that matched the `%' in the pattern,
  1439.          so you can use $* in the commands.  */
  1440.       if (pattern != 0)
  1441.         {
  1442.           static char *percent = "%";
  1443.           char *buffer = variable_expand ("");
  1444.           char *o = patsubst_expand (buffer, name, pattern, percent,
  1445.                      pattern_percent, percent);
  1446.           f->stem = savestring (buffer, o - buffer);
  1447.         }
  1448.     }
  1449.       else
  1450.     {
  1451.       /* Double-colon.  Make a new record
  1452.          even if the file already has one.  */
  1453.       f = lookup_file (name);
  1454.       /* Check for both : and :: rules.  Check is_target so
  1455.          we don't lose on default suffix rules or makefiles.  */
  1456.       if (f != 0 && f->is_target && !f->double_colon)
  1457.         makefile_fatal (filename, lineno,
  1458.                 "target file `%s' has both : and :: entries",
  1459.                 f->name);
  1460.       f = enter_file (name);
  1461.       /* If there was an existing entry and it was a double-colon
  1462.          entry, enter_file will have returned a new one, making it the
  1463.          prev pointer of the old one, and setting its double_colon
  1464.          pointer to the first one.  */
  1465.       if (f->double_colon == 0)
  1466.         /* This is the first entry for this name, so we must
  1467.            set its double_colon pointer to itself.  */
  1468.         f->double_colon = f;
  1469.       f->is_target = 1;
  1470.       f->deps = this;
  1471.       f->cmds = cmds;
  1472.     }
  1473.  
  1474.       /* Free name if not needed further.  */
  1475.       if (f != 0 && name != f->name
  1476.       && (name < f->name || name > f->name + strlen (f->name)))
  1477.     {
  1478.       free (name);
  1479.       name = f->name;
  1480.     }
  1481.  
  1482.       /* See if this is first target seen whose name does
  1483.      not start with a `.', unless it contains a slash.  */
  1484.       if (default_goal_file == 0 && set_default
  1485.       && (*name != '.' || index (name, '/') != 0
  1486. #ifdef __MSDOS__
  1487.                || index (name, '\\') != 0
  1488. #endif
  1489.           ))
  1490.     {
  1491.       int reject = 0;
  1492.  
  1493.       /* If this file is a suffix, don't
  1494.          let it be the default goal file.  */
  1495.  
  1496.       for (d = suffix_file->deps; d != 0; d = d->next)
  1497.         {
  1498.           register struct dep *d2;
  1499.           if (*dep_name (d) != '.' && streq (name, dep_name (d)))
  1500.         {
  1501.           reject = 1;
  1502.           break;
  1503.         }
  1504.           for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
  1505.         {
  1506.           register unsigned int len = strlen (dep_name (d2));
  1507.           if (strncmp (name, dep_name (d2), len))
  1508.             continue;
  1509.           if (streq (name + len, dep_name (d)))
  1510.             {
  1511.               reject = 1;
  1512.               break;
  1513.             }
  1514.         }
  1515.           if (reject)
  1516.         break;
  1517.         }
  1518.  
  1519.       if (!reject)
  1520.         default_goal_file = f;
  1521.     }
  1522.     }
  1523.  
  1524.   if (implicit)
  1525.     {
  1526.       targets[target_idx] = 0;
  1527.       target_percents[target_idx] = 0;
  1528.       create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
  1529.       free ((char *) target_percents);
  1530.     }
  1531. }
  1532.  
  1533. /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
  1534.    Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
  1535.    Quoting backslashes are removed from STRING by compacting it into
  1536.    itself.  Returns a pointer to the first unquoted STOPCHAR if there is
  1537.    one, or nil if there are none.  */
  1538.  
  1539. char *
  1540. find_char_unquote (string, stopchars, blank)
  1541.      char *string;
  1542.      char *stopchars;
  1543.      int blank;
  1544. {
  1545.   unsigned int string_len = 0;
  1546.   register char *p = string;
  1547.  
  1548.   while (1)
  1549.     {
  1550.       while (*p != '\0' && index (stopchars, *p) == 0
  1551.          && (!blank || !isblank (*p)))
  1552.     ++p;
  1553.       if (*p == '\0')
  1554.     break;
  1555.  
  1556.       if (p > string && p[-1] == '\\')
  1557.     {
  1558.       /* Search for more backslashes.  */
  1559.       register int i = -2;
  1560.       while (&p[i] >= string && p[i] == '\\')
  1561.         --i;
  1562.       ++i;
  1563.       /* Only compute the length if really needed.  */
  1564.       if (string_len == 0)
  1565.         string_len = strlen (string);
  1566.       /* The number of backslashes is now -I.
  1567.          Copy P over itself to swallow half of them.  */
  1568.       bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
  1569.       p += i / 2;
  1570.       if (i % 2 == 0)
  1571.         /* All the backslashes quoted each other; the STOPCHAR was
  1572.            unquoted.  */
  1573.         return p;
  1574.  
  1575.       /* The STOPCHAR was quoted by a backslash.  Look for another.  */
  1576.     }
  1577.       else
  1578.     /* No backslash in sight.  */
  1579.     return p;
  1580.     }
  1581.  
  1582.   /* Never hit a STOPCHAR or blank (with BLANK nonzero).  */
  1583.   return 0;
  1584. }
  1585.  
  1586. /* Search PATTERN for an unquoted %.  */
  1587.  
  1588. char *
  1589. find_percent (pattern)
  1590.      char *pattern;
  1591. {
  1592.   return find_char_unquote (pattern, "%", 0);
  1593. }
  1594.  
  1595. /* Parse a string into a sequence of filenames represented as a
  1596.    chain of struct nameseq's in reverse order and return that chain.
  1597.  
  1598.    The string is passed as STRINGP, the address of a string pointer.
  1599.    The string pointer is updated to point at the first character
  1600.    not parsed, which either is a null char or equals STOPCHAR.
  1601.  
  1602.    SIZE is how big to construct chain elements.
  1603.    This is useful if we want them actually to be other structures
  1604.    that have room for additional info.
  1605.  
  1606.    If STRIP is nonzero, strip `./'s off the beginning.  */
  1607.  
  1608. struct nameseq *
  1609. parse_file_seq (stringp, stopchar, size, strip)
  1610.      char **stringp;
  1611.      int stopchar;
  1612.      unsigned int size;
  1613.      int strip;
  1614. {
  1615.   register struct nameseq *new = 0;
  1616.   register struct nameseq *new1, *lastnew1;
  1617.   register char *p = *stringp;
  1618.   char *q;
  1619.   char *name;
  1620.   char stopchars[3];
  1621.  
  1622. #ifdef VMS
  1623.   stopchars[0] = ',';
  1624.   stopchars[1] = stopchar;
  1625.   stopchars[2] = '\0';
  1626. #else
  1627.   stopchars[0] = stopchar;
  1628.   stopchars[1] = '\0';
  1629. #endif
  1630.  
  1631.   while (1)
  1632.     {
  1633.       /* Skip whitespace; see if any more names are left.  */
  1634.       p = next_token (p);
  1635.       if (*p == '\0')
  1636.     break;
  1637.       if (*p == stopchar)
  1638.     break;
  1639.  
  1640.       /* Yes, find end of next name.  */
  1641.       q = p;
  1642.       p = find_char_unquote (q, stopchars, 1);
  1643. #ifdef VMS
  1644.     /* convert comma separated list to space separated */
  1645.       if (p && *p == ',')
  1646.     *p =' ';
  1647. #endif
  1648. #ifdef __MSDOS__
  1649.       /* For MS-DOS, skip a "C:\..." or a "C:/..." until we find a
  1650.      first colon which isn't followed by a slash or a backslash.  */
  1651.       if (stopchar == ':')
  1652.     while (p != 0 && (p[1] == '\\' || p[1] == '/') && isalpha (p[-1]))
  1653.       p = find_char_unquote (p + 1, stopchars, 1);
  1654. #endif
  1655. #ifdef _AMIGA
  1656.       if (stopchar == ':' && p && *p == ':' &&
  1657.     !(isspace(p[1]) || !p[1] || isspace(p[-1])))
  1658.       {
  1659.     p = find_char_unquote (p+1, stopchars, 1);
  1660.       }
  1661. #endif
  1662. #ifdef WINDOWS32
  1663.       /* For WINDOWS32, skip a "C:\..." or "C:/...". */
  1664.       if (stopchar == ':' &&
  1665.           p != 0 &&
  1666.           (p[1] == '\\' || p[1] == '/') &&
  1667.           isalpha (p[-1])) {
  1668.         p = end_of_token_w32(++p, ':');
  1669.         if (*p == '\0' && p[-1] == ':')
  1670.           p--;
  1671.       }
  1672. #endif
  1673.       if (p == 0)
  1674.     p = q + strlen (q);
  1675.  
  1676.       if (strip)
  1677. #ifdef VMS
  1678.     /* Skip leading `[]'s.  */
  1679.     while (p - q > 2 && q[0] == '[' && q[1] == ']')
  1680. #else
  1681.     /* Skip leading `./'s.  */
  1682.     while (p - q > 2 && q[0] == '.' && q[1] == '/')
  1683. #endif
  1684.       {
  1685.         q += 2;        /* Skip "./".  */
  1686.         while (q < p && *q == '/')
  1687.           /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
  1688.           ++q;
  1689.       }
  1690.  
  1691.       /* Extract the filename just found, and skip it.  */
  1692.  
  1693.       if (q == p)
  1694.     /* ".///" was stripped to "". */
  1695. #ifdef VMS
  1696.     continue;
  1697. #else
  1698. #ifdef _AMIGA
  1699.     name = savestring ("", 0);
  1700. #else
  1701.     name = savestring ("./", 2);
  1702. #endif
  1703. #endif
  1704.       else
  1705. #ifdef VMS
  1706. /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
  1707.  *  to remove this '\' before we can use the filename.
  1708.  * Savestring called because q may be read-only string constant.
  1709.  */
  1710.     {
  1711.       char *qbase = savestring(q, strlen(q));
  1712.       char *pbase = qbase + (p-q);
  1713.       char *q1 = qbase;
  1714.       char *q2 = q1;
  1715.       char *p1 = pbase;
  1716.  
  1717.       while (q1 != pbase)
  1718.         {
  1719.           if (*q1 == '\\' && *(q1+1) == ':')
  1720.         {
  1721.           q1++;
  1722.           p1--;
  1723.         }
  1724.           *q2++ = *q1++;
  1725.         }
  1726.       name = savestring (qbase, p1 - qbase);
  1727.       free (qbase);
  1728.     }
  1729. #else
  1730.     name = savestring (q, p - q);
  1731. #endif
  1732.  
  1733.       /* Add it to the front of the chain.  */
  1734.       new1 = (struct nameseq *) xmalloc (size);
  1735.       new1->name = name;
  1736.       new1->next = new;
  1737.       new = new1;
  1738.     }
  1739.  
  1740. #ifndef NO_ARCHIVES
  1741.  
  1742.   /* Look for multi-word archive references.
  1743.      They are indicated by a elt ending with an unmatched `)' and
  1744.      an elt further down the chain (i.e., previous in the file list)
  1745.      with an unmatched `(' (e.g., "lib(mem").  */
  1746.  
  1747.   new1 = new;
  1748.   lastnew1 = 0;
  1749.   while (new1 != 0)
  1750.     if (new1->name[0] != '('    /* Don't catch "(%)" and suchlike.  */
  1751.     && new1->name[strlen (new1->name) - 1] == ')'
  1752.     && index (new1->name, '(') == 0)
  1753.       {
  1754.     /* NEW1 ends with a `)' but does not contain a `('.
  1755.        Look back for an elt with an opening `(' but no closing `)'.  */
  1756.  
  1757.     struct nameseq *n = new1->next, *lastn = new1;
  1758.     char *paren;
  1759.     while (n != 0 && (paren = index (n->name, '(')) == 0)
  1760.       {
  1761.         lastn = n;
  1762.         n = n->next;
  1763.       }
  1764.     if (n != 0
  1765.         /* Ignore something starting with `(', as that cannot actually
  1766.            be an archive-member reference (and treating it as such
  1767.            results in an empty file name, which causes much lossage).  */
  1768.         && n->name[0] != '(')
  1769.       {
  1770.         /* N is the first element in the archive group.
  1771.            Its name looks like "lib(mem" (with no closing `)').  */
  1772.  
  1773.         char *libname;
  1774.  
  1775.         /* Copy "lib(" into LIBNAME.  */
  1776.         ++paren;
  1777.         libname = (char *) alloca (paren - n->name + 1);
  1778.         bcopy (n->name, libname, paren - n->name);
  1779.         libname[paren - n->name] = '\0';
  1780.  
  1781.         if (*paren == '\0')
  1782.           {
  1783.         /* N was just "lib(", part of something like "lib( a b)".
  1784.            Edit it out of the chain and free its storage.  */
  1785.         lastn->next = n->next;
  1786.         free (n->name);
  1787.         free ((char *) n);
  1788.         /* LASTN->next is the new stopping elt for the loop below.  */
  1789.         n = lastn->next;
  1790.           }
  1791.         else
  1792.           {
  1793.         /* Replace N's name with the full archive reference.  */
  1794.         name = concat (libname, paren, ")");
  1795.         free (n->name);
  1796.         n->name = name;
  1797.           }
  1798.  
  1799.         if (new1->name[1] == '\0')
  1800.           {
  1801.         /* NEW1 is just ")", part of something like "lib(a b )".
  1802.            Omit it from the chain and free its storage.  */
  1803.         if (lastnew1 == 0)
  1804.           new = new1->next;
  1805.         else
  1806.           lastnew1->next = new1->next;
  1807.         lastn = new1;
  1808.         new1 = new1->next;
  1809.         free (lastn->name);
  1810.         free ((char *) lastn);
  1811.           }
  1812.         else
  1813.           {
  1814.         /* Replace also NEW1->name, which already has closing `)'.  */
  1815.         name = concat (libname, new1->name, "");
  1816.         free (new1->name);
  1817.         new1->name = name;
  1818.         new1 = new1->next;
  1819.           }
  1820.  
  1821.         /* Trace back from NEW1 (the end of the list) until N
  1822.            (the beginning of the list), rewriting each name
  1823.            with the full archive reference.  */
  1824.  
  1825.         while (new1 != n)
  1826.           {
  1827.         name = concat (libname, new1->name, ")");
  1828.         free (new1->name);
  1829.         new1->name = name;
  1830.         lastnew1 = new1;
  1831.         new1 = new1->next;
  1832.           }
  1833.       }
  1834.     else
  1835.       {
  1836.         /* No frobnication happening.  Just step down the list.  */
  1837.         lastnew1 = new1;
  1838.         new1 = new1->next;
  1839.       }
  1840.       }
  1841.     else
  1842.       {
  1843.     lastnew1 = new1;
  1844.     new1 = new1->next;
  1845.       }
  1846.  
  1847. #endif
  1848.  
  1849.   *stringp = p;
  1850.   return new;
  1851. }
  1852.  
  1853. /* Read a line of text from STREAM into LINEBUFFER.
  1854.    Combine continuation lines into one line.
  1855.    Return the number of actual lines read (> 1 if hacked continuation lines).
  1856.  */
  1857.  
  1858. static unsigned int
  1859. readline (linebuffer, stream, filename, lineno)
  1860.      struct linebuffer *linebuffer;
  1861.      FILE *stream;
  1862.      char *filename;
  1863.      unsigned int lineno;
  1864. {
  1865.   char *buffer = linebuffer->buffer;
  1866.   register char *p = linebuffer->buffer;
  1867.   register char *end = p + linebuffer->size;
  1868.   register int len, lastlen = 0;
  1869.   register char *p2;
  1870.   register unsigned int nlines = 0;
  1871.   register int backslash;
  1872.  
  1873.   *p = '\0';
  1874.  
  1875.   while (fgets (p, end - p, stream) != 0)
  1876.     {
  1877.       len = strlen (p);
  1878.       if (len == 0)
  1879.     {
  1880.       /* This only happens when the first thing on the line is a '\0'.
  1881.          It is a pretty hopeless case, but (wonder of wonders) Athena
  1882.          lossage strikes again!  (xmkmf puts NULs in its makefiles.)
  1883.          There is nothing really to be done; we synthesize a newline so
  1884.          the following line doesn't appear to be part of this line.  */
  1885.       makefile_error (filename, lineno,
  1886.               "warning: NUL character seen; rest of line ignored");
  1887.       p[0] = '\n';
  1888.       len = 1;
  1889.     }
  1890.  
  1891.       p += len;
  1892.       if (p[-1] != '\n')
  1893.     {
  1894.       /* Probably ran out of buffer space.  */
  1895.       register unsigned int p_off = p - buffer;
  1896.       linebuffer->size *= 2;
  1897.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  1898.       p = buffer + p_off;
  1899.       end = buffer + linebuffer->size;
  1900.       linebuffer->buffer = buffer;
  1901.       *p = '\0';
  1902.       lastlen = len;
  1903.       continue;
  1904.     }
  1905.  
  1906.       ++nlines;
  1907.  
  1908.       if (len == 1 && p > buffer)
  1909.     /* P is pointing at a newline and it's the beginning of
  1910.        the buffer returned by the last fgets call.  However,
  1911.        it is not necessarily the beginning of a line if P is
  1912.        pointing past the beginning of the holding buffer.
  1913.        If the buffer was just enlarged (right before the newline),
  1914.        we must account for that, so we pretend that the two lines
  1915.        were one line.  */
  1916.     len += lastlen;
  1917.       lastlen = len;
  1918.       backslash = 0;
  1919.       for (p2 = p - 2; --len > 0; --p2)
  1920.     {
  1921.       if (*p2 == '\\')
  1922.         backslash = !backslash;
  1923.       else
  1924.         break;
  1925.     }
  1926.  
  1927.       if (!backslash)
  1928.     {
  1929.       p[-1] = '\0';
  1930.       break;
  1931.     }
  1932.  
  1933.       if (end - p <= 1)
  1934.     {
  1935.       /* Enlarge the buffer.  */
  1936.       register unsigned int p_off = p - buffer;
  1937.       linebuffer->size *= 2;
  1938.       buffer = (char *) xrealloc (buffer, linebuffer->size);
  1939.       p = buffer + p_off;
  1940.       end = buffer + linebuffer->size;
  1941.       linebuffer->buffer = buffer;
  1942.     }
  1943.     }
  1944.  
  1945.   if (ferror (stream))
  1946.     pfatal_with_name (filename);
  1947.  
  1948.   return nlines;
  1949. }
  1950.  
  1951. /* Construct the list of include directories
  1952.    from the arguments and the default list.  */
  1953.  
  1954. void
  1955. construct_include_path (arg_dirs)
  1956.      char **arg_dirs;
  1957. {
  1958.   register unsigned int i;
  1959. #ifdef VAXC        /* just don't ask ... */
  1960.   stat_t stbuf;
  1961. #else
  1962.   struct stat stbuf;
  1963. #endif
  1964.   /* Table to hold the dirs.  */
  1965.  
  1966.   register unsigned int defsize = (sizeof (default_include_directories)
  1967.                    / sizeof (default_include_directories[0]));
  1968.   register unsigned int max = 5;
  1969.   register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
  1970.   register unsigned int idx = 0;
  1971.  
  1972. #ifdef  __MSDOS__
  1973.   defsize++;
  1974. #endif
  1975.  
  1976.   /* First consider any dirs specified with -I switches.
  1977.      Ignore dirs that don't exist.  */
  1978.  
  1979.   if (arg_dirs != 0)
  1980.     while (*arg_dirs != 0)
  1981.       {
  1982.     char *dir = *arg_dirs++;
  1983.  
  1984.     if (dir[0] == '~')
  1985.       {
  1986.         char *expanded = tilde_expand (dir);
  1987.         if (expanded != 0)
  1988.           dir = expanded;
  1989.       }
  1990.  
  1991.     if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
  1992.       {
  1993.         if (idx == max - 1)
  1994.           {
  1995.         max += 5;
  1996.         dirs = (char **)
  1997.           xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
  1998.           }
  1999.         dirs[idx++] = dir;
  2000.       }
  2001.     else if (dir != arg_dirs[-1])
  2002.       free (dir);
  2003.       }
  2004.  
  2005.   /* Now add at the end the standard default dirs.  */
  2006.  
  2007. #ifdef  __MSDOS__
  2008.   {
  2009.     /* The environment variable $DJDIR holds the root of the
  2010.        DJGPP directory tree; add ${DJDIR}/include.  */
  2011.     struct variable *djdir = lookup_variable ("DJDIR", 5);
  2012.  
  2013.     if (djdir)
  2014.       {
  2015.     char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
  2016.  
  2017.     strcat (strcpy (defdir, djdir->value), "/include");
  2018.     dirs[idx++] = defdir;
  2019.       }
  2020.   }
  2021. #endif
  2022.  
  2023.   for (i = 0; default_include_directories[i] != 0; ++i)
  2024.     if (stat (default_include_directories[i], &stbuf) == 0
  2025.     && S_ISDIR (stbuf.st_mode))
  2026.       dirs[idx++] = default_include_directories[i];
  2027.  
  2028.   dirs[idx] = 0;
  2029.  
  2030.   /* Now compute the maximum length of any name in it.  */
  2031.  
  2032.   max_incl_len = 0;
  2033.   for (i = 0; i < idx; ++i)
  2034.     {
  2035.       unsigned int len = strlen (dirs[i]);
  2036.       /* If dir name is written with a trailing slash, discard it.  */
  2037.       if (dirs[i][len - 1] == '/')
  2038.     /* We can't just clobber a null in because it may have come from
  2039.        a literal string and literal strings may not be writable.  */
  2040.     dirs[i] = savestring (dirs[i], len - 1);
  2041.       if (len > max_incl_len)
  2042.     max_incl_len = len;
  2043.     }
  2044.  
  2045.   include_directories = dirs;
  2046. }
  2047.  
  2048. /* Expand ~ or ~USER at the beginning of NAME.
  2049.    Return a newly malloc'd string or 0.  */
  2050.  
  2051. char *
  2052. tilde_expand (name)
  2053.      char *name;
  2054. {
  2055. #ifndef VMS
  2056.   if (name[1] == '/' || name[1] == '\0')
  2057.     {
  2058.       extern char *getenv ();
  2059.       char *home_dir;
  2060.       int is_variable;
  2061.  
  2062.       {
  2063.     /* Turn off --warn-undefined-variables while we expand HOME.  */
  2064.     int save = warn_undefined_variables_flag;
  2065.     warn_undefined_variables_flag = 0;
  2066.  
  2067.     home_dir = allocated_variable_expand ("$(HOME)");
  2068.  
  2069.     warn_undefined_variables_flag = save;
  2070.       }
  2071.  
  2072.       is_variable = home_dir[0] != '\0';
  2073.       if (!is_variable)
  2074.     {
  2075.       free (home_dir);
  2076.       home_dir = getenv ("HOME");
  2077.     }
  2078. #if !defined(_AMIGA) && !defined(WINDOWS32)
  2079.       if (home_dir == 0 || home_dir[0] == '\0')
  2080.     {
  2081.       extern char *getlogin ();
  2082.       char *name = getlogin ();
  2083.       home_dir = 0;
  2084.       if (name != 0)
  2085.         {
  2086.           struct passwd *p = getpwnam (name);
  2087.           if (p != 0)
  2088.         home_dir = p->pw_dir;
  2089.         }
  2090.     }
  2091. #endif /* !AMIGA && !WINDOWS32 */
  2092.       if (home_dir != 0)
  2093.     {
  2094.       char *new = concat (home_dir, "", name + 1);
  2095.       if (is_variable)
  2096.         free (home_dir);
  2097.       return new;
  2098.     }
  2099.     }
  2100. #if !defined(_AMIGA) && !defined(WINDOWS32)
  2101.   else
  2102.     {
  2103.       struct passwd *pwent;
  2104.       char *userend = index (name + 1, '/');
  2105.       if (userend != 0)
  2106.     *userend = '\0';
  2107.       pwent = getpwnam (name + 1);
  2108.       if (pwent != 0)
  2109.     {
  2110.       if (userend == 0)
  2111.         return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
  2112.       else
  2113.         return concat (pwent->pw_dir, "/", userend + 1);
  2114.     }
  2115.       else if (userend != 0)
  2116.     *userend = '/';
  2117.     }
  2118. #endif /* !AMIGA && !WINDOWS32 */
  2119. #endif /* !VMS */
  2120.   return 0;
  2121. }
  2122.  
  2123. /* Given a chain of struct nameseq's describing a sequence of filenames,
  2124.    in reverse of the intended order, return a new chain describing the
  2125.    result of globbing the filenames.  The new chain is in forward order.
  2126.    The links of the old chain are freed or used in the new chain.
  2127.    Likewise for the names in the old chain.
  2128.  
  2129.    SIZE is how big to construct chain elements.
  2130.    This is useful if we want them actually to be other structures
  2131.    that have room for additional info.  */
  2132.  
  2133. struct nameseq *
  2134. multi_glob (chain, size)
  2135.      struct nameseq *chain;
  2136.      unsigned int size;
  2137. {
  2138.   extern void dir_setup_glob ();
  2139.   register struct nameseq *new = 0;
  2140.   register struct nameseq *old;
  2141.   struct nameseq *nexto;
  2142.   glob_t gl;
  2143.  
  2144.   dir_setup_glob (&gl);
  2145.  
  2146.   for (old = chain; old != 0; old = nexto)
  2147.     {
  2148. #ifndef NO_ARCHIVES
  2149.       char *memname;
  2150. #endif
  2151.  
  2152.       nexto = old->next;
  2153.  
  2154.       if (old->name[0] == '~')
  2155.     {
  2156.       char *newname = tilde_expand (old->name);
  2157.       if (newname != 0)
  2158.         {
  2159.           free (old->name);
  2160.           old->name = newname;
  2161.         }
  2162.     }
  2163.  
  2164. #ifndef NO_ARCHIVES
  2165.       if (ar_name (old->name))
  2166.     {
  2167.       /* OLD->name is an archive member reference.
  2168.          Replace it with the archive file name,
  2169.          and save the member name in MEMNAME.
  2170.          We will glob on the archive name and then
  2171.          reattach MEMNAME later.  */
  2172.       char *arname;
  2173.       ar_parse_name (old->name, &arname, &memname);
  2174.       free (old->name);
  2175.       old->name = arname;
  2176.     }
  2177.       else
  2178.     memname = 0;
  2179. #endif /* !NO_ARCHIVES */
  2180.  
  2181.       switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
  2182.     {
  2183.     case 0:            /* Success.  */
  2184.       {
  2185.         register int i = gl.gl_pathc;
  2186.         while (i-- > 0)
  2187.           {
  2188. #ifndef NO_ARCHIVES
  2189.         if (memname != 0)
  2190.           {
  2191.             /* Try to glob on MEMNAME within the archive.  */
  2192.             struct nameseq *found
  2193.               = ar_glob (gl.gl_pathv[i], memname, size);
  2194.             if (found == 0)
  2195.               {
  2196.             /* No matches.  Use MEMNAME as-is.  */
  2197.             struct nameseq *elt
  2198.               = (struct nameseq *) xmalloc (size);
  2199.             unsigned int alen = strlen (gl.gl_pathv[i]);
  2200.             unsigned int mlen = strlen (memname);
  2201.             elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
  2202.             bcopy (gl.gl_pathv[i], elt->name, alen);
  2203.             elt->name[alen] = '(';
  2204.             bcopy (memname, &elt->name[alen + 1], mlen);
  2205.             elt->name[alen + 1 + mlen] = ')';
  2206.             elt->name[alen + 1 + mlen + 1] = '\0';
  2207.             elt->next = new;
  2208.             new = elt;
  2209.               }
  2210.             else
  2211.               {
  2212.             /* Find the end of the FOUND chain.  */
  2213.             struct nameseq *f = found;
  2214.             while (f->next != 0)
  2215.               f = f->next;
  2216.  
  2217.             /* Attach the chain being built to the end of the FOUND
  2218.                chain, and make FOUND the new NEW chain.  */
  2219.             f->next = new;
  2220.             new = found;
  2221.               }
  2222.  
  2223.             free (memname);
  2224.           }
  2225.         else
  2226. #endif /* !NO_ARCHIVES */
  2227.           {
  2228.             struct nameseq *elt = (struct nameseq *) xmalloc (size);
  2229.             elt->name = savestring (gl.gl_pathv[i],
  2230.                         strlen (gl.gl_pathv[i]));
  2231.             elt->next = new;
  2232.             new = elt;
  2233.           }
  2234.           }
  2235.         globfree (&gl);
  2236.         free (old->name);
  2237.         free ((char *)old);
  2238.         break;
  2239.       }
  2240.  
  2241.     case GLOB_NOSPACE:
  2242.       fatal ("virtual memory exhausted");
  2243.       break;
  2244.  
  2245.     default:
  2246.       old->next = new;
  2247.       new = old;
  2248.       break;
  2249.     }
  2250.     }
  2251.  
  2252.   return new;
  2253. }
  2254.